package com.zhongfl.crm.lixiao.config.task;


import com.zhongfl.crm.lixiao.config.JobSwitchConfigProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import tech.heptagram.core.common.batch.BatchExecutor;

import javax.annotation.Resource;

/**
 * 任务调度
 *
 * @author tiantian
 * @date 2018/4/12
 */
@Component
@Slf4j
public class LxScheduledTask {

    @Resource
    private JobSwitchConfigProperties jobSwitchConfigProperties;

    @Resource(name = "lxDepartmentSyncJobExecutor")
    private BatchExecutor lxDepartmentSyncJobExecutor;

    @Resource(name = "lxUserSyncJobExecutor")
    private BatchExecutor lxUserSyncJobExecutor;

    @Resource(name = "lxChannelSyncJobExecutor")
    private BatchExecutor lxChannelSyncJobExecutor;

    @Resource(name = "orderSyncFromLX2BossJobExecutor")
    private BatchExecutor orderSyncFromLX2BossJobExecutor;

    @Resource(name = "orderRevisitLogSyncFromLX2BossJobExecutor")
    private BatchExecutor orderRevisitLogSyncFromLX2BossJobExecutor;

    @Resource(name = "lxChannelRevisitLog2BossSyncJobExecutor")
    private BatchExecutor lxChannelRevisitLog2BossSyncJobExecutor;

    @Resource(name = "orderSyncFromBoss2LXTaskExecutor")
    private BatchExecutor orderSyncFromBoss2LXTaskExecutor;

    @Resource(name = "orderRevisitLogSyncFromBoss2LXTaskExecutor")
    private BatchExecutor orderRevisitLogSyncFromBoss2LXTaskExecutor;

    /**
     * 励销部门 同步任务调度入口 CRM->BOSS
     */
    @Scheduled(cron = "0 0 0/1 * * ?")
    public void lixiaoDepartmentSyncJob() {
        if (jobSwitchConfigProperties.getLx2boss().isDepartment()) {
            lxDepartmentSyncJobExecutor.execute();
        } else {
            log.info("Job: lixiaoDepartmentSyncJob 开关没开, 任务跳过");
        }
    }

    /**
     * 励销用户 同步任务调度入口 CRM->BOSS
     */
    @Scheduled(cron = "30 30 0/1 * * ?")
    public void lixiaoUserSyncJob() {
        if (jobSwitchConfigProperties.getLx2boss().isUser()) {
            lxUserSyncJobExecutor.execute();
        } else {
            log.info("Job: lixiaoUserSyncJob 开关没开, 任务跳过");
        }
    }

    /**
     * 订单同步 CRM->BOSS
     */
    @Scheduled(cron = "0 0/5 * * * ?")
    public void orderSyncFromLX2BossJob() {
        if (jobSwitchConfigProperties.getLx2boss().isCustomerOrder()) {
            orderSyncFromLX2BossJobExecutor.execute();
        } else {
            log.info("Job: orderSyncFromLX2BossJob 开关没开, 任务跳过");
        }
    }

    /**
     * 订单同步 BOSS->CRM
     */
    @Scheduled(cron = "40 0/1 * * * ?")
    public void orderSyncFromBoss2LXTask() {
        if (jobSwitchConfigProperties.getBoss2lx().isCustomerOrder()) {
            orderSyncFromBoss2LXTaskExecutor.execute();
        } else {
            log.info("Job: orderSyncFromBoss2LXTask 开关没开, 任务跳过");
        }
    }

    /**
     * 渠道同步 CRM->BOSS
     * 每5分钟同步一次, 在1分钟 6分钟触发
     * 秒针指向40触发, 给用户留下20秒的时间(貌似没什么用)
     */
    @Scheduled(cron = "40 1/5 * * * ?")
    public void lxChannelSyncJob() {
        if (jobSwitchConfigProperties.getLx2boss().isChannel()) {
            lxChannelSyncJobExecutor.execute();
        } else {
            log.info("Job: lxChannelSyncJobExecutor 开关没开, 任务跳过");
        }
    }

    /**
     * 订单跟进记录 BOSS->CRM
     */
    @Scheduled(cron = "35 0/2 * * * ?")
    public void orderRevisitLogSyncFromBoss2LXTask() {
        if (jobSwitchConfigProperties.getBoss2lx().isRevisitLog()) {
            orderRevisitLogSyncFromBoss2LXTaskExecutor.execute();
        } else {
            log.info("Job: orderRevisitLogSyncFromBoss2LXTask 开关没开, 任务跳过");
        }
    }

    /**
     * 订单跟进记录 CRM->BOSS
     */
    @Scheduled(cron = "30 0/5 * * * ?")
    public void orderRevisitLogSyncFromLX2BossJob() {
        if (jobSwitchConfigProperties.getLx2boss().isRevisitLog()) {
            orderRevisitLogSyncFromLX2BossJobExecutor.execute();
        } else {
            log.info("Job: orderRevisitLogSyncFromLX2BossJob 开关没开, 任务跳过");
        }
    }

    /**
     * 渠道跟进记录 CRM->BOSS
     */
    @Scheduled(cron = "40 5 0/1 * * ?")
    public void lxChannelRevisitLog2BossSyncJob() {
        if (jobSwitchConfigProperties.getLx2boss().isRevisitLog()) {
            lxChannelRevisitLog2BossSyncJobExecutor.execute();
        } else {
            log.info("Job: lxChannelRevisitLog2BossSyncJob 开关没开, 任务跳过");
        }
    }
}
